<template>
  <div class="app-container">
    <div class="wrap">
      <div class="mg-bottom-10">
        <el-row :gutter="20">
          <el-col :span="5">
            <el-row :gutter="10">
              <el-col :span="6"><div class="lh-28 fz-14">政策标题</div></el-col>
              <el-col :span="18">
                <el-input v-model="searchFrom.title" />
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="5">
            <el-row :gutter="10">
              <el-col :span="6"><div class="lh-28 fz-14">政策内容</div></el-col>
              <el-col :span="18">
                <el-input v-model="searchFrom.content"></el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="5">
            <el-row :gutter="10">
              <el-col :span="6"><div class="lh-28 fz-14">政策来源</div></el-col>
              <el-col :span="18">
                <el-input v-model="searchFrom.source"></el-input>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="5">
            <el-row :gutter="10">
              <el-col :span="6"><div class="lh-28 fz-14">发布时间</div></el-col>
              <el-col :span="18">
                <el-date-picker
                  v-model="searchFrom.releasedTime"
                  type="date"
                  placeholder="选择日期"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                ></el-date-picker>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="4">
            <el-button type="info" size="small " @click="getList">查询</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="flex-row justify-between">
        <div class="flex-row">
          <custom-button class="custom-button" @click="addNews"></custom-button>
          <!-- 新增 -->
        </div>
      </div>
      <div class="flex-sub table-container">
        <el-table
          v-loading="listLoading"
          :data="list"
          element-loading-text="Loading"
          header-cell-class-name="table-header-cell"
          fit
          highlight-current-row
          :max-height="600"
        >
          <el-table-column align="center" label="序号" width="95">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="政策标题">
            <template slot-scope="scope">
              {{ scope.row.title }}
            </template>
          </el-table-column>
          <el-table-column label="政策内容">
            <template slot-scope="scope">
              <div class="o">
                {{ scope.row.content }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="政策来源" width="95">
            <template slot-scope="scope">
              {{ scope.row.source }}
            </template>
          </el-table-column>
          <el-table-column label="创建时间" width="160" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime | dateTimeFormatter  }}</span>
            </template>
          </el-table-column>
          <el-table-column label="更新周期" width="110" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.renewalCycle }} 小时</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="110" align="center">
            <template slot-scope="scope">
              <el-dropdown trigger="click">
                <span class="el-dropdown-link">更多</span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item divided @click.native="showDetail(scope.row)">
                    详 情
                  </el-dropdown-item>
                  <el-dropdown-item divided @click.native="update(scope.row)">
                    修改
                  </el-dropdown-item>
                  <el-dropdown-item divided @click.native="delNews(scope.row.id)">
                    删 除
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            background
            :total="total"
            :page-size="paging.pageSize"
            layout="total, prev, pager, next, jumper"
            :current-page="paging.pageNo"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
    <!-- 添加/修改 -->
    <dialogNews
      :dialogTit="dialogTit"
      :formVisible.sync="formVisible"
      :newsId="newsId"
      :newsType="searchFrom.newsType"
      @getList="getList"
    ></dialogNews>
    <!-- 详情 -->
    <InfoDetail :visible.sync="detailedShow" :detail="detailedShowData"></InfoDetail>
  </div>
</template>

<script>
import { findCompanyList, findCompanyDelete } from '@/child-business/api/news'
import { handleScreenWidthToVw } from '@/utils/tools'
import InfoDetail from '@/child-business/components/infoDetails'
import dialogNews from './components/dialogNews' // 弹窗部分
export default {
  name: 'Policy',
  components: { InfoDetail, dialogNews },
  data() {
    return {
      detailedShow: false,
      detailedShowData: {},
      searchFrom: {
        releasedTime: '',
        source: '',
        title: '',
        content: '',
        newsType: 2,
      },
      upBtnShow: false,
      dialogTit: '修改内容',
      formVisible: false,
      newsId: '', // 传入ID
      list: [],
      listLoading: true,
      hasMOre: false,
      moreSearchIf: false,
      record: '',
      paging: {
        pageNo: 1,
        pageSize: 12,
      },
      total: 0,
    }
  },
  computed: {
    handleScreenWidthToVw() {
      return handleScreenWidthToVw
    },
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 添加政策
    addNews() {
      this.newsId = ''
      this.formVisible = true
      this.dialogTit = '添加政策'
    },
    // 获取政策
    getList() {
      this.listLoading = true
      findCompanyList({ ...this.paging, companyNewsVo: this.searchFrom }).then(response => {
        console.log(response)
        this.listLoading = false
        this.list = response.records
        this.paging.pageNo = response.pageNo
        this.paging.pageSize = response.pageSize
        this.total = response.total
      })
    },
    // 点击删除
    delNews(id) {
      this.$confirm('确定删除该条数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          findCompanyDelete({ id: id }).then(() => {
            this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!',
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 点击分页
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getList()
    },
    // 跳转分页
    handleCurrentChange(val) {
      this.paging.pageNo = val
      this.getList()
    },
    // 详情
    showDetail(row) {
      this.detailedShow = true
      this.detailedShowData = row
    },
    // 修改
    update(row) {
      this.newsId = row.id
      this.formVisible = true
    },
    // 删除
    deleteMsg(id) {
      console.log('删除', id)
    },
  },
}
</script>

<style lang="scss" scoped>
@import '@/child-business/styles/public.scss';
@import '@/child-business/styles/news.scss';
</style>
